//
// Created by tonystark on 16/3/22.
// Copyright (c) 2016 sewm. All rights reserved.
//

#import "MessageController.h"

#define SearchBarHeight 44
#define BottomBarHeight 50

@interface MessageController () <UISearchBarDelegate> {

}
@property(nonatomic, strong) UISearchBar *searchBar;
@property(nonatomic, strong) YouDaoSearchResultsView *searchResultsView;
@end

@implementation MessageController {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.searchBar];
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(SearchBarHeight);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).mas_offset(SearchBarHeight);
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.right.equalTo(self.view);
    }];
}

- (NSString *)topTitle {
    return @"消息";
}

#pragma mark  search

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        _searchBar.placeholder = @"搜索";
        _searchBar.delegate = self;
        _searchBar.barTintColor = UIColorFromRGB(0xA7A7A7);
        _searchBar.translucent = NO;
        [[[[_searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
        _searchBar.backgroundColor = UIColorFromRGB(0xeff0f0);

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, SearchBarHeight - 1, deviceWidth, 1)];
        line.backgroundColor = UIColorFromRGB(0xE4E4E4);
        [_searchBar addSubview:line];

        [self searchResultsView];
    }
    return _searchBar;
}

- (YouDaoSearchResultsView *)searchResultsView {
    if (!_searchResultsView) {
        _searchResultsView = [[YouDaoSearchResultsView alloc] init];
        _searchResultsView.delegate = self;
        _searchResultsView.searchResultsDataSource = self;
        _searchResultsView.searchResultsDelegate = self;
    }
    return _searchResultsView;
}

#pragma mark UITableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

#pragma mark - UISearchDisplayDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    if (searchBar == self.searchBar) {
        //立即停止滚动
        [self.tableView forceStopScrolling];
        [self.searchResultsView showWithBlock:^{
            [self.view addSubview:self.searchResultsView];
        }                        startedPoint:CGPointMake(0, topBarHeight)];
        return NO;
    } else {
        return YES;
    }
}

- (void)dishSearchResultsView:(YouDaoSearchResultsView *)view filterWithSearchString:(NSString *)searchString {

}

@end